package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;

public class registerPage extends Utils {
    public static String url="http://49.235.61.184:8080/user/login";

    public registerPage()
    {

        super(url);
    }


    /**
     *
     1.检查注册页面是否可以正常打开（跳转）
     *
     **/
    public  void checkPageRight()  {
        String curUrl = driver.getCurrentUrl();
        //点击注册账户，是否跳转到注册页面
        wait.until(ExpectedConditions.elementToBeClickable(By.linkText("注册账户"))).click();// 用于 <a> 标签的文本定位
        assert curUrl.equals("http://49.235.61.184:8080/user/register");

        //检查中/英切换
        driver.findElement(By.cssSelector("#root > div > div.lang___3VyRW > span > i > svg > path.css-c4d79v"));
        //登录框
        driver.findElement(By.cssSelector("#name"));
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#rePassword"));
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(6) > a"));
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button"));

        //点击返回登录，是否跳转到登录页面
        wait.until(ExpectedConditions.elementToBeClickable(By.linkText("返回登录"))).click();// 用于 <a> 标签的文本定位
        assert curUrl.equals("http://49.235.61.184:8080/user/login");

    }

    /**
     2.注册成功---输入正确的姓名、账号、密码和密码确认
     */
    public void registerSucc() throws IOException {
        //先刷新页面, 清空输入框
        driver.navigate().refresh();

        driver.findElement(By.cssSelector("#name")).sendKeys("abc");
        driver.findElement(By.cssSelector("#username")).sendKeys("ppp");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        //点击注册后进入登录界面


        //获取当前页面url
        String url = driver.getCurrentUrl();
        //如果已经跳转至登录界面, 则注册成功
        assert url.equals("http://49.235.61.184:8080/user/login");

        //屏幕截图
        getScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());



    }

    /**
     3.注册失败
     * ----姓名、账号、密码和密码确认其中有为空
     * ----账号已存在
     * ----密码和确认密码不一致
     */
    public  void registerFail(){
        String curUrl = driver.getCurrentUrl();
        //回到注册页面
        wait.until(ExpectedConditions.elementToBeClickable(By.linkText("注册账户"))).click();// 用于 <a> 标签的文本定位
        assert curUrl.equals("http://49.235.61.184:8080/user/register");

        //姓名为空，登录账户为空
        driver.findElement(By.cssSelector("#name")).sendKeys("");
        driver.findElement(By.cssSelector("#username")).sendKeys("");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        //处理错误提示---- 刷新页面
        driver.navigate().refresh();

        //账号已存在
        driver.findElement(By.cssSelector("#name")).sendKeys("abc");
        driver.findElement(By.cssSelector("#username")).sendKeys("1");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        //处理错误提示---- 刷新页面
        driver.navigate().refresh();

        //密码和确认密码不一致
        driver.findElement(By.cssSelector("#name")).sendKeys("abc");
        driver.findElement(By.cssSelector("#username")).sendKeys("kkk");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        //点击注册按钮，还在注册页面
        String curUrl1 = driver.getCurrentUrl();
        //如果点击注册后, 当前仍在注册界面, 则注册失败
        assert curUrl1.equals("http://49.235.61.184:8080/user/register");

    }

}
